package com.bailiban.base.controller;

import com.bailiban.base.entity.Label;
import com.bailiban.base.service.LabelService;
import com.bailiban.common.entity.PageResult;
import com.bailiban.common.entity.Result;
import com.bailiban.common.entity.StatusCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

/**
 * @ClassName LabelController
 * @Description TODO
 * @Author ThinkPad
 * @Date 2020/11/16 16:15
 * @Version 1.0
 **/
@RestController
@RequestMapping("label")
public class LabelController {
    @Autowired
    private LabelService labelService;

    @GetMapping("{id}")
    public Result queryById(@PathVariable String id){
        Label label = labelService.queryById(id);
        return new Result(true, StatusCode.OK,"根据id查询成功！",label);
    }
    @GetMapping
    public Result findAll(){
        List<Label> all = labelService.findAll();
        return new Result(true, StatusCode.OK,"查询成功！",all);
    }
    @PostMapping("search/{page}/{size}")
    public Result search(@RequestBody Map map,@PathVariable int page,@PathVariable int size){
        Page search = labelService.search(map, page, size);
        return new Result(true, StatusCode.OK,"分页查询查询成功！",new PageResult<Label>(search.getTotal(),search.getRecords()));
    }
    @PostMapping
    public Result save(@RequestBody Label label){
        labelService.save(label);
        return new Result(true, StatusCode.OK,"新增成功！");
    }
    @PutMapping("{id}")
    public Result update(@RequestBody Label label,@PathVariable String id){
        label.setId(id);
        labelService.queryById(id);
        return new Result(true, StatusCode.OK,"更新成功！");
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable String id){
        labelService.delete(id);
        return new Result(true, StatusCode.OK,"删除成功！");
    }
}
